package edu.stu.mall.activity.recommended

import android.app.Application
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import edu.mall.base.BaseViewModel
import edu.mall.base.SingleLiveEvent
import edu.stu.mall.repository.Repository
import edu.stu.mall.repository.data.Product
import edu.stu.mall.repository.data.ProductAttributeValue
import edu.stu.mall.repository.data.ProductDetailData
import edu.stu.mall.repository.data.RecommendedProductDataItem
import edu.stu.mall.repository.data.SkuStock
import kotlinx.coroutines.launch

class ProductDetailViewHolder(application: Application) : BaseViewModel(application) {

    val productDetailData = MutableLiveData<ProductDetailData>()

    val productAttributeValueMap = MutableLiveData<Map<String, String>>()


    fun getProductDetailData(productId: String) {
        viewModelScope.launch {
            Repository.getProductDetail(productId)?.let { data ->
                val map = HashMap<String, String>()
                data.productAttributeValueList?.forEach { attributeValue: ProductAttributeValue ->
                    data.productAttributeList?.forEach {
                        if (attributeValue.productAttributeId == it.id) {
                            map[it.name!!] = attributeValue.value!!
                        }
                    }
                }
                productAttributeValueMap.postValue(map)

                productDetailData.postValue(data)
            }
        }
    }
}

